package com.qf.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.core.vo.R;
import com.qf.user.entity.Menu;
import com.qf.user.mapper.MenuMapper;
import com.qf.user.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author yxh
 * @since 2023-10-29
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public R selectAllMenus() {
        //先查询一级菜单
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Menu::getParentId,"0");
        List<Menu> menus = this.baseMapper.selectList(queryWrapper);

        //根据一级菜单的id去查二级菜单
        for (Menu menu : menus) {
            List<Menu> children = this.baseMapper.children(menu.getId());
            if (children != null) {
                menu.setChildren(children);
            }
        }

        return R.ok(menus);
    }

    @Override
    public R add(Menu menu) {
        if (menu == null) {
            return R.fail("参数错误");
        }
        menu.setCreateTime(new Date());
        this.baseMapper.insert(menu);
        return R.ok();
    }

    @Override
    public R selectMenus() {
        List<Menu> menus = this.baseMapper.selectMenus();
        return R.ok(menus,"查询成功");
    }
}
